class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable, registerable
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable,
         :validatable, :confirmable

  has_many :sales

  # role: admin, manager, stuff
  validates :role, inclusion: { in: %w(admin manager staff) }
  # TODO: phone, email
  validates :wechat_openid, uniqueness: true, if: 'wechat_openid.present?'
  validates :wechat_unionid, uniqueness: true, if: 'wechat_unionid.present?'


  def name_or_email
    self.name || self.email
  end

  def role_name
    return unless role

    Hash[[['staff', '员工'], ['manager', '操作员'], ['admin', '管理员']]][role]
  end

  def admin?
    role == 'admin'
  end
end
